#include "zgsprite.h"
#include <vector>

int main() {
    Window::Init(800, 600, "贪吃蛇游戏");

    // 蛇头和身体
    Sprite snakeHead("fonts/1.png");
    snakeHead.SetColor(GREEN);
    snakeHead.SetPosition(0, 0);
    snakeHead.SetScale(0.5);

    std::vector<Sprite> snakeBody; // 存储蛇的身体

    Sprite food;
    food.SetColor(RED);
    food.SetScale(0.5);
    food.SetPosition(0, 0);

    Pen pen;
    pen.SetColor(BLUE);

    int score = 0;
    float speed = 5.0f;
    float direction = 0.0f;

    while (!Window::ShouldClose()) {
        // 处理输入
        if (Keyboard::IsKeyPressed(KEY_RIGHT) && direction != 180) direction = 0;
        if (Keyboard::IsKeyPressed(KEY_DOWN) && direction != 270) direction = 90;
        if (Keyboard::IsKeyPressed(KEY_LEFT) && direction != 0) direction = 180;
        if (Keyboard::IsKeyPressed(KEY_UP) && direction != 90) direction = 270;

        // 记录蛇头上一帧的位置
        Vector2 prevHeadPos = snakeHead.GetPosition();

        // 移动蛇头
        if (direction == 0) snakeHead.Move(speed, 0);
        if (direction == 90) snakeHead.Move(0, speed);
        if (direction == 180) snakeHead.Move(-speed, 0);
        if (direction == 270) snakeHead.Move(0, -speed);

        // 移动蛇的身体
        if (!snakeBody.empty()) {
            for (size_t i = snakeBody.size() - 1; i > 0; --i) {
                Vector2 prevPos = snakeBody[i - 1].GetPosition();
                snakeBody[i].SetPosition(prevPos.x, prevPos.y); // 显式传递 x 和 y
            }
            snakeBody[0].SetPosition(prevHeadPos.x, prevHeadPos.y); // 显式传递 x 和 y
        }

        // 检测食物碰撞
        if (snakeHead.IsColliding(food)) {
            score += 10;
            food.SetPosition(GetRandomValue(-350, 350), GetRandomValue(-250, 250));
            speed += 0.2f;

            // 每次吃到食物增加3节身体，每节间隔10像素
            for (int i = 0; i < 4; i++) {
                Sprite newBody;
                newBody.SetColor(GREEN);
                if (snakeBody.empty()) {
                    // 初始状态：新身体节与蛇头上一帧位置相同
                    newBody.SetPosition(prevHeadPos.x, prevHeadPos.y);
                }
                else {
                    // 后续状态：新身体节与尾部保持10像素间隔
                    Vector2 tailPos = snakeBody.back().GetPosition();
                    // 根据蛇的移动方向计算偏移量
                    float offsetX = 0, offsetY = 0;
                    if (direction == 0) offsetX = -10;      // 向右移动时，新身体节向左偏移
                    else if (direction == 90) offsetY = -10; // 向下移动时，新身体节向上偏移
                    else if (direction == 180) offsetX = 10;  // 向左移动时，新身体节向右偏移
                    else if (direction == 270) offsetY = 10;  // 向上移动时，新身体节向下偏移
                    newBody.SetPosition(tailPos.x + offsetX, tailPos.y + offsetY);
                }
                snakeBody.push_back(newBody);
            }
        }

        // 边界检测
        Vector2 pos = snakeHead.GetPosition();
        if (pos.x < -400 || pos.x > 400 || pos.y < -300 || pos.y > 300) {
            snakeHead.SetPosition(0, 0);
            snakeBody.clear(); // 清空身体
            food.SetPosition(200, 200);
            score = 0;
            speed = 5.0f;
            direction = 0;
        }

        // 绘制
        Window::BeginDraw();
        for (auto& body : snakeBody) { // 移除 const 限定
            body.Draw();
        }
        snakeHead.Draw();        
        food.Draw();

        char scoreText[64];
        sprintf(scoreText, "score: %d", score);
        pen.DrawText("吃到红色方块，加分。", { -400,250 }, 24, BLUE, "fonts/LXGWWenKaiMonoScreen.ttf");
        pen.DrawText(scoreText, { 2,20 }, 24, BLUE, "fonts/LXGWWenKaiMonoScreen.ttf");
        pen.DrawText("↑↓←→: 蛇运动", { 2,80 }, 24, BLUE, "fonts/LXGWWenKaiMonoScreen.ttf");
        Window::EndDraw();
    }

    Window::Close();
    return 0;
}